package com.qys.livesMall.shop.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccount;
import com.qys.livesMall.shop.mapper.ShopPurchaseInfoMapper;
import com.qys.livesMall.shop.modal.dto.ShopPurchaseAllDto;
import com.qys.livesMall.shop.modal.dto.ShopPurchaseDto;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopPurchaseInfo;
import com.qys.livesMall.shop.modal.entity.ShopPurchaseSku;
import com.qys.livesMall.shop.modal.query.QueryPurchase;
import com.qys.livesMall.shop.service.H2ShopBaseService;
import com.qys.livesMall.shop.service.ShopPurchaseInfoService;
import com.qys.livesMall.shop.service.ShopPurchaseSkuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *采购
 * @author
 * @since 2023-06-06
 */
@Tag(name = "采购", description = "采购")
@RestController
@RequestMapping("/sys/shopPurchaseInfo")
public class SysShopPurchaseInfoController {
    @Resource
    private ShopPurchaseInfoService purchaseInfoService;

    @Resource
    private ShopPurchaseSkuService purchaseSkuService;

    @Resource
    private H2ShopBaseService shopBaseService;

    @Resource
    private ShopPurchaseInfoMapper purchaseInfoMapper;
    @PostMapping("/page")
    public R<Page<ShopPurchaseDto>> getList(@RequestBody QueryPurchase data){
        if(StringUtils.isNotBlank(data.getShopName())){

        }
        List<H2ShopBase> shops=null;
        if (StringUtils.isNotBlank(data.getShopName())) {
            shops = shopBaseService.likeByShopName(data.getShopName());
            if (CollectionUtils.isEmpty(shops)){
                return R.ok(new Page<>());
            }
            List<Long> shopIds = shops.stream().map(H2ShopBase::getId).collect(Collectors.toList());
            data.setShopIds(shopIds);
        }

        Page<ShopPurchaseDto> page = Page.of(data.getCurrent(), data.getSize());

        Page<ShopPurchaseDto> purchaseDtos = purchaseInfoMapper.getPurchaseInfoWithSku(page,data);
        if(CollectionUtils.isEmpty(purchaseDtos.getRecords())){
            return R.ok(new Page<>());
        }
        if (CollectionUtils.isEmpty(shops)){
            List<Long> shopIds = page.getRecords().stream().map(ShopPurchaseDto::getShopId).collect(Collectors.toList());
            shops = shopBaseService.listByIds(shopIds);
        }

        Map<Long, H2ShopBase> shopBaseMap = shops.stream().collect(Collectors.toMap(H2ShopBase::getId, v -> v));

        for (ShopPurchaseDto record : purchaseDtos.getRecords()) {
            H2ShopBase h2ShopBase=shopBaseMap.get(record.getShopId());
            if (null != h2ShopBase){
                record.setShopName(h2ShopBase.getShopName());
            }
        }
        return R.ok(purchaseDtos);
    }

    @GetMapping("/{id}/{skuId}")
    public R<ShopPurchaseAllDto> get(@PathVariable Long id,@PathVariable Long skuId){
        ShopPurchaseInfo shopPurchaseInfo=purchaseInfoService.getById(id);
        ShopPurchaseSku sku = purchaseSkuService.getById(skuId);
        ShopPurchaseAllDto shopPurchase = new ShopPurchaseAllDto();
        shopPurchase.setInfo(shopPurchaseInfo);
        shopPurchase.setSku(sku);
        return R.ok(shopPurchase);
    }

}
